package cn.huanba.service;

import cn.huanba.dao.UserDao;
import cn.huanba.entity.User;
import cn.huanba.utils.RedisUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-01-07 17:23:28
 */
@RestController
@RequestMapping("user")
public class UserServiceImpl {
    @Resource
    private UserDao userDao;
    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("loginByLoginname")
    public User loginByLoginname(@RequestParam("loginname") String loginname, @RequestParam("password") String password) {
        System.out.println("----provider-2");
        System.out.println("延迟5秒,测试熔断");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userDao.loginByLoginname(loginname, password);
    }

    @RequestMapping("loginByPhone")
    public User loginByPhone(@RequestParam("phone") String phone) {
        return userDao.loginByPhone(phone);
    }

    @RequestMapping("getUserByToken")
    public User getUserByToken(@RequestParam("token") String token) {
        User account = null;
        System.out.println("provider:token:"+token);
        String value = redisUtils.get(token);
        if (value != null) {
            account = JSONObject.parseObject(value, User.class);
        }
        return account;
    }
}